<?php
/*
// "K2" Component by JoomlaWorks for Joomla! 1.5.x - Version 2.1
// Copyright (c) 2006 - 2009 JoomlaWorks Ltd. All rights reserved.
// Released under the GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
// More info at http://www.joomlaworks.gr and http://k2.joomlaworks.gr
// Designed and developed by the JoomlaWorks team
// *** Last update: September 9th, 2009 ***
*/

/*
// mod for K2 Extra fields Filter and Search module by Piotr Konieczny
// piotr@smartwebstudio.com
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

class K2ModelItemlistFilter extends JModel {

    function prepareFilter($search) {
        jimport('joomla.filesystem.file');
        $db = &JFactory::getDBO();
        $language = &JFactory::getLanguage();
        $defaultLang = $language->getDefault();
        $currentLang = $language->getTag();
        $conditions = array();
        $search_ignore = array();
        $sql = '';
        
        $ignoreFile = $language->getLanguagePath().DS.$currentLang.DS.$currentLang.'.ignore.php';
        
        if (JFile::exists($ignoreFile)) {
            include $ignoreFile;
        }
        $length = JString::strlen($search);
		
        if (JString::substr($search, 0, 1) == '"' && JString::substr($search, $length - 1, 1) == '"') {
        
            $word = JString::substr($search, 1, $length - 2);
            
            if (JString::strlen($word) > 3 && !in_array($word, $search_ignore)) {
                $word = $db->Quote('%'.$db->getEscaped($word, true).'%', false);
                
                if (JFolder::exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_joomfish') && $currentLang != $defaultLang && JRequest::getVar('all_langs') != 'yes') {
                
                    $jfQuery = " SELECT reference_id FROM #__jf_content as jfc LEFT JOIN #__languages as jfl ON jfc.language_id = jfl.id";
                    $jfQuery .= " WHERE jfc.reference_table = 'k2_items'";
                    $jfQuery .= " AND jfl.code=".$db->Quote($currentLang);
                    $jfQuery .= " AND jfc.published=1";
                    $jfQuery .= " AND jfc.value LIKE ".$word;
                    $jfQuery .= " AND (jfc.reference_field = 'title' 
								OR jfc.reference_field = 'extra_fields_search'
								OR jfc.reference_field = 'introtext'
								OR jfc.reference_field = 'fulltext'
								OR jfc.reference_field = 'image_caption'
								OR jfc.reference_field = 'image_credits'
								OR jfc.reference_field = 'video_caption'
								OR jfc.reference_field = 'video_credits'
								OR jfc.reference_field = 'metadesc'
								OR jfc.reference_field = 'metakey'
					)";
                    $db->setQuery($jfQuery);
                    $result = $db->loadResultArray();
                    $result = @array_unique($result);
                    if (count($result)) {
                        $conditions[] = "i.id IN(".implode(',', $result).")";
                    }
                    
                } else {
					$conditions[] = 'i.extra_fields_search LIKE '.$word;
					if (JRequest::getInt('field_id') != '') {
                    $conditions[] = 'i.title LIKE '.$word;
                    $conditions[] = 'i.introtext LIKE '.$word;
                    $conditions[] = 'i.fulltext LIKE '.$word;
                    $conditions[] = 'i.image_caption LIKE '.$word;
                    $conditions[] = 'i.image_credits LIKE '.$word;
                    $conditions[] = 'i.video_caption LIKE '.$word;
                    $conditions[] = 'i.video_credits LIKE '.$word;
                    $conditions[] = 'i.metadesc LIKE '.$word;
                    $conditions[] = 'i.metakey LIKE '.$word;
					}
                }
                
            }
            
        } else { 
            $search = explode(' ', JString::strtolower($search));
            foreach ($search as $searchword) {
            
                if (JString::strlen($searchword) > 3 && !in_array($searchword, $search_ignore)) {
                
                    $word = $db->Quote('%'.$db->getEscaped($searchword, true).'%', false);

                    if (JFolder::exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_joomfish') && $currentLang != $defaultLang && JRequest::getVar('all_langs') != 'yes') {
                        $jfQuery = " SELECT reference_id FROM #__jf_content as jfc LEFT JOIN #__languages as jfl ON jfc.language_id = jfl.id";
                        $jfQuery .= " WHERE jfc.reference_table = 'k2_items'";
                        $jfQuery .= " AND jfl.code=".$db->Quote($currentLang);
                        $jfQuery .= " AND jfc.published=1";
                        $jfQuery .= " AND jfc.value LIKE ".$word;
                        $jfQuery .= " AND (jfc.reference_field = 'title' 
									OR jfc.reference_field = 'extra_fields_search'
									OR jfc.reference_field = 'introtext'
									OR jfc.reference_field = 'fulltext'
									OR jfc.reference_field = 'image_caption'
									OR jfc.reference_field = 'image_credits'
									OR jfc.reference_field = 'video_caption'
									OR jfc.reference_field = 'video_credits'
									OR jfc.reference_field = 'metadesc'
									OR jfc.reference_field = 'metakey'
						)";
                        $db->setQuery($jfQuery);
                        $result = $db->loadResultArray();
                        $result = @array_unique($result);
                        foreach ($result as $id) {
                            $allIDs[] = $id;
                        }

                        
                    } else {
						$conditions[] = 'i.extra_fields_search LIKE '.$word;
						if (JRequest::getInt('field_id') != '') {
                        $conditions[] = 'i.title LIKE '.$word;
                        $conditions[] = 'i.introtext LIKE '.$word;
                        $conditions[] = 'i.fulltext LIKE '.$word;
                        $conditions[] = 'i.image_caption LIKE '.$word;
                        $conditions[] = 'i.image_credits LIKE '.$word;
                        $conditions[] = 'i.video_caption LIKE '.$word;
                        $conditions[] = 'i.video_credits LIKE '.$word;
                        $conditions[] = 'i.metadesc LIKE '.$word;
                        $conditions[] = 'i.metakey LIKE '.$word;
						}
                    }
                    
                    if (JFolder::exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_joomfish') && $currentLang != $defaultLang) {
                    
                        if (isset($allIDs) && count($allIDs)) {
                            $conditions[] = "i.id IN(".implode(',', $allIDs).")";
                        }
                        
                    }

                    
                }
                
            }

            
        }
        
        if (count($conditions)) {
            $sql = " AND (".implode(" OR ", $conditions).")";
        }
        return $sql;
    }
    
}
